package com.thz.system.controller.exam;

import com.thz.common.core.controller.BaseController;
import com.thz.common.core.enums.ResultCode;
import com.thz.common.core.pojo.TableDataInfo;
import com.thz.common.core.result.Result;
import com.thz.exception.ApplicationException;
import com.thz.system.pojo.exam.dto.ExamAddDTO;
import com.thz.system.pojo.exam.dto.ExamEditDTO;
import com.thz.system.pojo.exam.dto.ExamQueryDTO;
import com.thz.system.pojo.exam.dto.ExamQuestAddDTO;
import com.thz.system.pojo.exam.vo.ExamDetailVO;
import com.thz.system.pojo.homework.dto.HomeworkAddDTO;
import com.thz.system.pojo.homework.dto.HomeworkQueryDTO;
import com.thz.system.pojo.homework.vo.HomeworkQuestionVO;
import com.thz.system.pojo.homework.vo.HomeworkScoreVO;
import com.thz.system.pojo.homework.dto.HomeworkUserDTO;
import com.thz.system.service.exam.IExamService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author thz
 * @Date 2025/3/24
 */
@RestController
@RequestMapping("/exam")
@Tag(name = "题目竞赛接口")
public class ExamController extends BaseController {

    @Autowired
    private IExamService examService;

    @GetMapping("/list")
    public TableDataInfo list(ExamQueryDTO examQueryDTO) {
        return getTableDataInfo(examService.list(examQueryDTO));
    }

    @GetMapping("/listHomework")
    public TableDataInfo listHomework(HomeworkQueryDTO homeworkQueryDTO) {
        return getTableDataInfo(examService.listHomework(homeworkQueryDTO));
    }

    @PostMapping("/addHomework")
    public Result<String> addHomework(@RequestBody HomeworkAddDTO homeworkAddDTO) {
        if(homeworkAddDTO.getTitle() == null || homeworkAddDTO.getTitle().isEmpty()) {
            throw new ApplicationException(Result.failed("作业标题不能为空"));
        }

        if(homeworkAddDTO.getStartTime() == null){
            throw new ApplicationException(Result.failed("作业开始时间不能为空"));
        }

        if(homeworkAddDTO.getEndTime() == null) {
            throw new ApplicationException(Result.failed("作业结束时间不能为空"));
        }
        return Result.ok(examService.addHomework(homeworkAddDTO));
    }

    @PostMapping("/add")
    public Result<String> add(@RequestBody ExamAddDTO examAddDTO) {
        if(examAddDTO.getTitle() == null || examAddDTO.getTitle().isEmpty()) {
            throw new ApplicationException(Result.failed("竞赛标题不能为空"));
        }

        if(examAddDTO.getStartTime() == null){
            throw new ApplicationException(Result.failed("竞赛开始时间不能为空"));
        }

        if(examAddDTO.getEndTime() == null) {
            throw new ApplicationException(Result.failed("竞赛结束时间不能为空"));
        }

        return Result.ok(examService.add(examAddDTO));
    }


    @PostMapping("/question/add")
    public Result questionAdd(@RequestBody ExamQuestAddDTO examQuestAddDTO) {
        return ToResult(examService.questionAdd(examQuestAddDTO));
    }

    @DeleteMapping("/question/delete")
    public Result questionDelete(@NonNull @RequestParam("examId") Long examId,@NonNull @RequestParam("questionId") Long questionId) {
        return DToResult(examService.questionDelete(examId, questionId));
    }

    @GetMapping("/detail")
    public Result<ExamDetailVO> detail(@NonNull @RequestParam("examId") Long examId) {
        return Result.ok(examService.detail(examId));
    }

    @PutMapping("/edit")
    public Result edit(@RequestBody ExamEditDTO examEditDTO) {
        return UToResult(examService.edit(examEditDTO));
    }

    @PutMapping("/editHomework")
    public Result editHomework(@RequestBody ExamEditDTO examEditDTO) {
        return UToResult(examService.editHomework(examEditDTO));
    }

    @DeleteMapping("/delete")
    public Result delete(@NonNull @RequestParam("examId") Long examId) {
        return DToResult(examService.delete(examId));
    }

    @DeleteMapping("/deleteHomework")
    public Result deleteHomework(@NonNull @RequestParam("examId") Long examId) {
        return DToResult(examService.deleteHomework(examId));
    }

    @GetMapping("/getHomeworkScore")
    public Result<List<HomeworkScoreVO>> getHomeworkScore(@NonNull @RequestParam("examId") Long examId, @NonNull @RequestParam("courseId") Long courseId) {
        return Result.ok(examService.getHomeworkScore(examId,courseId));
    }

    @PutMapping("/publish")
    public Result publish(@NonNull @RequestParam("examId") Long examId) {
        return UToResult(examService.publish(examId));
    }

    @PutMapping("/publishHomework")
    public Result publishHomework(@NonNull @RequestParam("examId") Long examId) {
        return UToResult(examService.publishHomework(examId));
    }

    @PutMapping("/cancelPublish")
    public Result cancelPublish(Long examId) {
        return UToResult(examService.cancelPublish(examId));
    }

    @PutMapping("/cancelPublishHomework")
    public Result cancelPublishHomework(Long examId) {
        return UToResult(examService.cancelPublishHomework(examId));
    }

    @GetMapping("/getHomeworkQuestionVO")
    public Result<List<HomeworkQuestionVO>> getHomeworkQuestionVO(@NonNull @RequestParam("examId") Long examId) {
        return Result.ok(examService.getHomeworkQuestionVO(examId));
    }

    @GetMapping("/getHomeworkUserDTO")
    public Result<List<HomeworkUserDTO>> getHomeworkUserDTO(@NonNull @RequestParam("questionId") Long questionId,
                                                            @NonNull @RequestParam("courseId") Long courseId,
                                                            @NonNull @RequestParam("examId") Long examId) {
        return Result.ok(examService.getHomeworkUserDTO(questionId,courseId,examId));
    }
}
